﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;

namespace SilkRoute
{
    public partial class exporter : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder content = new StringBuilder();

            //ConfigDB
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConfigDB"].ConnectionString);
            using (con)
            {
                SqlCommand cmd = new SqlCommand("select * from profile;", con);
                con.Open();

                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        content.AppendLine(reader.GetString(1).ToString() + "," + reader.GetString(2).ToString() + "," + reader.GetString(3).ToString());
                    }
                }

                reader.Close();
            }

            HttpResponse response = System.Web.HttpContext.Current.Response;
            response.ClearHeaders();
            response.AppendHeader("Content-Disposition", "attachment; filename=export.csv");
            response.ContentType = "text/csv";
            response.Write(content);
            response.End();

            content.Clear();
        }
    }
}